package com.bank;

import java.util.Scanner;

public class ConsoleBankSystem {
    private Bank bank;
    private Scanner scanner;

    public ConsoleBankSystem() {
        bank = new Bank("我的银行");
        scanner = new Scanner(System.in);
    }

    public void start() {
        boolean running = true;
        while (running) {
            printMenu();
            int choice = getUserChoice();

            switch (choice) {
                case 1:
                    createCustomer();
                    break;
                case 2:
                    listCustomers();
                    break;
                case 3:
                    createAccount();
                    break;
                case 4:
                    listAccounts();
                    break;
                case 5:
                    deposit();
                    break;
                case 6:
                    withdraw();
                    break;
                case 7:
                    transfer();
                    break;
                case 8:
                    viewTransactions();
                    break;
                case 9:
                    running = false;
                    System.out.println("感谢使用银行系统，再见！");
                    break;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
        scanner.close();
    }

    private void printMenu() {
        System.out.println("\n===== 银行管理系统 =====");
        System.out.println("1. 创建客户");
        System.out.println("2. 查看客户列表");
        System.out.println("3. 创建账户");
        System.out.println("4. 查看账户列表");
        System.out.println("5. 存款");
        System.out.println("6. 取款");
        System.out.println("7. 转账");
        System.out.println("8. 查看交易记录");
        System.out.println("9. 退出系统");
        System.out.println("=======================");
    }

    private int getUserChoice() {
        System.out.print("请选择操作 (1-9): ");
        while (!scanner.hasNextInt()) {
            System.out.print("输入无效，请输入数字 (1-9): ");
            scanner.next();
        }
        return scanner.nextInt();
    }

    private void createCustomer() {
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();
        System.out.print("请输入客户姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入客户地址: ");
        String address = scanner.nextLine();

        Customer customer = new Customer(customerId, name, address);
        bank.addCustomer(customer);
        System.out.println("客户创建成功！客户ID: " + customer.getCustomerId());
    }

    private void listCustomers() {
        System.out.println("\n===== 客户列表 =====");
        for (Customer customer : bank.getCustomers()) {
            System.out.printf("ID: %s, 姓名: %s, 地址: %s, 账户数: %d, 总余额: %.2f\n",
                    customer.getCustomerId(),
                    customer.getName(),
                    customer.getAddress(),
                    customer.getAccounts().size(),
                    customer.getTotalBalance());
        }
    }

    private void createAccount() {
        listCustomers();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();

        Customer customer = bank.findCustomerById(customerId);
        if (customer == null) {
            System.out.println("客户不存在！");
            return;
        }

        System.out.println("请选择账户类型:");
        System.out.println("1. 储蓄账户");
        System.out.println("2. 支票账户");
        int accountType = getUserChoice();

        System.out.print("请输入初始存款: ");
        double initialDeposit = scanner.nextDouble();

        Account account;
        if (accountType == 1) {
            System.out.print("请输入年利率 (%): ");
            double interestRate = scanner.nextDouble();
            account = new SavingsAccount("SA" + System.currentTimeMillis(), customer, interestRate);
        } else {
            System.out.print("请输入透支限额: ");
            double overdraftLimit = scanner.nextDouble();
            account = new CheckingAccount("CA" + System.currentTimeMillis(), customer, overdraftLimit);
        }

        customer.addAccount(account);
        account.deposit(initialDeposit);
        System.out.println("账户创建成功！账户号码: " + account.getAccountNumber());
    }

    private void listAccounts() {
        listCustomers();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();

        Customer customer = bank.findCustomerById(customerId);
        if (customer == null) {
            System.out.println("客户不存在！");
            return;
        }

        System.out.printf("\n===== %s的账户列表 =====\n", customer.getName());
        for (Account account : customer.getAccounts()) {
            System.out.printf("账号: %s, 类型: %s, 余额: %.2f\n",
                    account.getAccountNumber(),
                    account instanceof SavingsAccount ? "储蓄账户" : "支票账户",
                    account.getBalance());
        }
    }

    private void deposit() {
        listCustomers();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();

        Customer customer = bank.findCustomerById(customerId);
        if (customer == null) {
            System.out.println("客户不存在！");
            return;
        }

        listAccounts(customer);
        System.out.print("请输入账户号码: ");
        String accountNumber = scanner.nextLine();

        Account account = findAccount(customer, accountNumber);
        if (account == null) {
            System.out.println("账户不存在！");
            return;
        }

        System.out.print("请输入存款金额: ");
        double amount = scanner.nextDouble();

        try {
            account.deposit(amount);
            System.out.printf("存款成功！当前余额: %.2f\n", account.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }

    private void withdraw() {
        listCustomers();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入客户ID: ");
        String customerId = scanner.nextLine();

        Customer customer = bank.findCustomerById(customerId);
        if (customer == null) {
            System.out.println("客户不存在！");
            return;
        }

        listAccounts(customer);
        System.out.print("请输入账户号码: ");
        String accountNumber = scanner.nextLine();

        Account account = findAccount(customer, accountNumber);
        if (account == null) {
            System.out.println("账户不存在！");
            return;
        }

        System.out.print("请输入取款金额: ");
        double amount = scanner.nextDouble();

        try {
            account.withdraw(amount);
            System.out.printf("取款成功！当前余额: %.2f\n", account.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }

    private void transfer() {
        listCustomers();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入转出客户ID: ");
        String fromCustomerId = scanner.nextLine();

        Customer fromCustomer = bank.findCustomerById(fromCustomerId);
        if (fromCustomer == null) {
            System.out.println("转出客户不存在！");
            return;
        }

        listAccounts(fromCustomer);
        System.out.print("请输入转出账户号码: ");
        String fromAccountNumber = scanner.nextLine();

        Account fromAccount = findAccount(fromCustomer, fromAccountNumber);
        if (fromAccount == null) {
            System.out.println("转出账户不存在！");
            return;
        }

        System.out.print("请输入转入客户ID: ");
        String toCustomerId = scanner.nextLine();

        Customer toCustomer = bank.findCustomerById(toCustomerId);
        if (toCustomer == null) {
            System.out.println("转入客户不存在！");
            return;
        }

        listAccounts(toCustomer);
        System.out.print("请输入转入账户号码: ");
        String toAccountNumber = scanner.nextLine();

        Account toAccount = findAccount(toCustomer, toAccountNumber);
        if (toAccount == null) {
            System.out.println("转入账户不存在！");
            return;
        }

        System.out.print("请输入转账金额: ");
        double amount = scanner.nextDouble();

        try {
            fromAccount.transfer(toAccount, amount);
            System.out.printf("转账成功！转出账户当前余额: %.2f，转入账户当前余额: %.2f\n", fromAccount.getBalance(), toAccount.getBalance());
        } catch (IllegalArgumentException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }

    private void viewTransactions() {
        // 由于原代码中没有实现交易记录功能，这里简单输出提示信息
        System.out.println("暂不支持查看交易记录功能。");
    }

    private void listAccounts(Customer customer) {
        System.out.printf("\n===== %s的账户列表 =====\n", customer.getName());
        for (Account account : customer.getAccounts()) {
            System.out.printf("账号: %s, 类型: %s, 余额: %.2f\n",
                    account.getAccountNumber(),
                    account instanceof SavingsAccount ? "储蓄账户" : "支票账户",
                    account.getBalance());
        }
    }

    private Account findAccount(Customer customer, String accountNumber) {
        for (Account account : customer.getAccounts()) {
            if (account.getAccountNumber().equals(accountNumber)) {
                return account;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ConsoleBankSystem system = new ConsoleBankSystem();
        system.start();
    }
}